package com.zjpavt.taiyuan.service.weakManager;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.zjpavt.taiyuan.service.weakManager.pojo.ControlStateSO;
import com.zjpavt.taiyuan.service.weakManager.pojo.DeviceStatusSO;
import com.zjpavt.taiyuan.service.weakManager.pojo.WeakParamSO;
import com.zjpavt.taiyuan.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.methods.PostMethod;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author caozhi
 * @Date 2020/1/14
 * @Description
 * @Param 
 * @Return 
 */
@Slf4j
@Service
public class WeakManagerServiceImpl implements WeakManagerService {
    private final String url = "http://10.20.30.50/outerapi/v1.htm";
    private final Gson gson = new Gson();

    @Override
    public boolean controlPlay(@Valid WeakParamSO weakParamSO) {
        try {
            PostMethod postMethod = new PostMethod(url);
            String result = HttpClientUtil.post(postMethod, gson.toJson(weakParamSO), null);
            JsonObject objectResult = gson.fromJson(result, JsonObject.class);
            if (objectResult != null && "OK".equals(objectResult.get("Result").getAsString())) {
                return true;
            }
        } catch (Exception e) {
            log.error("Http请求失败"+e.getMessage());
        }
        return false;
    }

    @Override
    public Map<String, String> getDeviceStatus(WeakParamSO weakParamSO) {
        try {
            Map<String, String> map = new HashMap<>();
            map.put("ProjectNO", weakParamSO.getProjectNO());
            map.put("Password", weakParamSO.getPassword());
            map.put("M8ClusterNO", weakParamSO.getM8ClusterNO());
            map.put("CommandNO", weakParamSO.getCommandNO());
            PostMethod postMethod = new PostMethod(url);
            String result = HttpClientUtil.post(postMethod, gson.toJson(map), null);
            JsonObject objectResult = gson.fromJson(result, JsonObject.class);
            if (objectResult != null && "OK".equals(objectResult.get("Result").getAsString())) {
                Map<String, String> data = gson.fromJson(objectResult.getAsJsonObject("data"), Map.class);
                if (data == null) {
                    data = new HashMap<>();
                }
                data.put("info", objectResult.get("info").getAsString());
                return data;
            }
        } catch (Exception e) {
            log.error("Http请求失败"+e.getMessage());
        }
        return null;
    }

    @Override
    public boolean playAll(WeakParamSO weakParamSO) {
        try {
            Map<String, String> map = new HashMap<>();
            map.put("ProjectNO", weakParamSO.getProjectNO());
            map.put("Password", weakParamSO.getPassword());
            map.put("M8ClusterNO", weakParamSO.getM8ClusterNO());
            map.put("CommandNO", weakParamSO.getCommandNO());
            map.put("StartTime", weakParamSO.getStartTime());
            map.put("Delay", weakParamSO.getDelay());
            PostMethod postMethod = new PostMethod(url);
            String result = HttpClientUtil.post(postMethod, gson.toJson(map), null);
            JsonObject objectResult = gson.fromJson(result, JsonObject.class);
            if (objectResult != null && "OK".equals(objectResult.get("Result").getAsString()) && Integer.parseInt(objectResult.get("info").getAsString())>0) {
                return true;
            }
        } catch (Exception e) {
            log.error("Http请求失败"+e.getMessage());
        }
        return false;
    }

    @Override
    public boolean scenePlay(WeakParamSO weakParamSO) {
        try {
            Map<String, String> map = new HashMap<>();
            map.put("ProjectNO", weakParamSO.getProjectNO());
            map.put("Password", weakParamSO.getPassword());
            map.put("M8ClusterNO", weakParamSO.getM8ClusterNO());
            map.put("CommandNO", weakParamSO.getCommandNO());
            map.put("StartTime", weakParamSO.getStartTime());
            map.put("Delay", weakParamSO.getDelay());
            PostMethod postMethod = new PostMethod(url);
            String result = HttpClientUtil.post(postMethod, gson.toJson(map), null);
            JsonObject objectResult = gson.fromJson(result, JsonObject.class);
            if (objectResult != null && "OK".equals(objectResult.get("Result").getAsString())) {
                return true;
            }
        } catch (Exception e) {
            log.error("Http请求失败"+e.getMessage());
        }
        return false;
    }

    @Override
    public String getPlaybackmode(WeakParamSO weakParamSO) {
        try {
            Map<String, String> map = new HashMap<>();
            map.put("ProjectNO", weakParamSO.getProjectNO());
            map.put("Password", weakParamSO.getPassword());
            map.put("M8ClusterNO", weakParamSO.getM8ClusterNO());
            map.put("CommandNO", weakParamSO.getCommandNO());
            PostMethod postMethod = new PostMethod(url);
            String result = HttpClientUtil.post(postMethod, gson.toJson(map), null);
            JsonObject objectResult = gson.fromJson(result, JsonObject.class);
            if (objectResult != null && "OK".equals(objectResult.get("Result").getAsString()) ) {
                return objectResult.get("info").getAsString();
            }
        } catch (Exception e) {
            log.error("Http请求失败"+e.getMessage());
        }
        return null;
    }

    @Override
    public ResultInfo getDeviceInformation(WeakParamSO weakParamSO) {
        try {
            Map<String, String> map = new HashMap<>();
            map.put("ProjectNO", weakParamSO.getProjectNO());
            map.put("Password", weakParamSO.getPassword());
            map.put("M8ClusterNO", weakParamSO.getM8ClusterNO());
            map.put("CommandNO", weakParamSO.getCommandNO());
            PostMethod postMethod = new PostMethod(url);
            String result = HttpClientUtil.post(postMethod, gson.toJson(map), null);
            ResultInfo resultInfo = gson.fromJson(result, ResultInfo.class);
            if (resultInfo != null && "OK".equals(resultInfo.getResult())) {
                List<DeviceStatusSO> data = resultInfo.getData();
                for (DeviceStatusSO datum : data) {
                    if (!"OFFLINE".equals(datum.getInfo())) {
                        String infoJson = gson.toJson(datum.getInfo());
                        List<ControlStateSO> controlStateSOS = gson.fromJson(infoJson,new TypeToken<List<ControlStateSO>>(){}.getType());
                    }
                }
            }
            return resultInfo;
        } catch (Exception e) {
            log.error("Http请求失败"+e.getMessage());
        }
        return null;
    }

    @Override
    public boolean addCustomArea(List<Map<String,Object>> param) {
        if (param != null) {
            PostMethod postMethod = new PostMethod();
            String result = null;
            try {
                result = HttpClientUtil.post(postMethod, gson.toJson(param), null);
                JsonObject objectResult = gson.fromJson(result, JsonObject.class);
                if ("OK".equals(objectResult.get("Result").getAsString())) {
                    return true;
                }
            } catch (Exception e) {
                log.error("Http请求失败" + e.getMessage());
                e.printStackTrace();
            }
        }
        return false;
    }
}